import { message } from 'antd';
import {
  reduceemployeeList,
  // reduceemployeeDetail,
  reduceemployeeReceipt,
  noticeSign,
  signPro,
  getProList,
  getProDetail,
  getFeedback,
  empTabNum,
  receiveTask,
  receiveordptinface,
  backReceipts,
  receiptModelification,
  recvdorderDetails,
  getAttritionProselectedList,
  cancelReceive,
  reduceCancelReceipt,
} from '@/services/receiveordermanage';

export default {
  state: {
    expandForm: false,
    activeKey: '1',
    formFieldsValue: {},
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    datar: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    proBaseList: {},
    proFbList: [],
    proListData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detailsData: {},
    proInsuranceList: [],
    tabList: [
      { itemName: '待接单', itemCode: '1' },
      { itemName: '已接单', itemCode: '2' },
      { itemName: '已回单', itemCode: '3' },
      { itemName: '问题人员', itemCode: '4' },
      { itemName: '已取消', itemCode: '5' },
    ],
    navNum: '1', // 导航数量
  },
  effects: {
    // 详情
    *feactorderDetail({ payload, callback }, { call, put }) {
      const response = yield call(recvdorderDetails, payload);
      const sortNumber = (a, b) => a.operLeafType - b.operLeafType;
      if (response.success) {
        if (response.data.execStatusDto && response.data.execStatusDto.leafList) {
          response.data.execStatusDto.leafList = response.data.execStatusDto.leafList.sort(
            sortNumber
          );
        }
        yield put({
          type: 'orderDetails',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '查询失败');
        yield put({
          type: 'orderDetails',
          payload: {},
        });
      }
      callback(response);
    },
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(reduceemployeeList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data || [],
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 回单
    *receiptId({ payload, callback }, { call }) {
      const response = yield call(backReceipts, payload);
      if (callback) {
        callback(response);
      }
    },
    // 回单
    *receiptback({ payload, callback }, { call, put }) {
      const response = yield call(receiveordptinface, payload);
      // const {} = payload;
      if (response.success) {
        yield put({
          type: 'receiptinfacemessage',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '查询失败');
      }
      callback(response);
    },
    // 减员回单修改
    *modification({ payload, callback }, { call }) {
      const response = yield call(receiptModelification, payload);
      if (callback) {
        callback(response);
      }
    },
    // 获取问题人员列表
    *fetchProList({ payload }, { call, put }) {
      const response = yield call(getProList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getProList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getProList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取页签数量
    *fetchTabNum({ payload }, { call, put, select }) {
      // const response = yield call(empTabNum, payload);
      // if (response.success) {
      //   if (callback && typeof callback === 'function') {
      //     callback(response.data);
      //   }
      // } else {
      //   message.warning(`${response.message || '服务端错误！'}`);
      // }
      const response = yield call(empTabNum, payload);
      let activeKey = yield select(state => state.reducemprec.activeKey);
      const tabList = yield select(state => state.reducemprec.tabList);
      if (response.success) {
        if (response.data && response.data.length) {
          const sortNumber = (a, b) => a.itemCode - b.itemCode;
          const tabCodeArr = response.data.sort(sortNumber);
          const isHasBadgeCurrentTab = tabCodeArr.some(item => `${item.tabHdl}` === activeKey); // 当前页签有没有查询到
          if (!isHasBadgeCurrentTab) {
            activeKey = `${tabCodeArr[0].tabHdl}`;
          }
          tabCodeArr.forEach(item => {
            if (`${item.tabHdl}` !== activeKey) {
              tabList[item.tabHdl - 1].isHas = true;
            }
          });
          yield put({
            type: 'updateModelData',
            payload: { activeKey, tabList },
          });
          const getListParams = {
            ...payload,
            pageArg: {
              currentPage: payload.currentPage,
              pageSize: payload.pageSize,
            },
          };
          if (activeKey === '4') {
            if (payload.formFieldsValue) {
              yield put({
                type: 'updateModelData',
                payload: {
                  ...payload.formFieldsValue,
                },
              });
            }
            yield put({
              type: 'problempage/getProList',
              payload: {
                ...getListParams,
                busiType: '9',
              },
            });
          } else {
            yield put({
              type: 'fetchList',
              payload: { ...getListParams, hdlState: activeKey },
            });
          }
        } else {
          yield put({
            type: 'getList',
            payload: {
              data: {},
              currentPage: 1,
              pageSize: 10,
            },
          });
          if (activeKey === '4') {
            yield put({
              type: 'problempage/updateProList',
              payload: {
                data: [],
                currentPage: 1,
                pageSize: 10,
              },
            });
          }
        }
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 发单
    *receiveTask({ payload }, { call }) {
      const response = yield call(receiveTask, payload);
      return response;
    },
    // 获取问题对象的险种
    *selectedRowsList({ payload }, { call, put }) {
      const payloads = {
        recvCutTaskIds: payload,
      };
      const response = yield call(getAttritionProselectedList, payloads);
      if (response.success) {
        yield put({
          type: 'getProInsuranceList',
          payload: response,
        });
      } else {
        message.warning(response.message || '查询失败！');
      }
    },
    // 获取问题人员详情
    *fetchProDetailList({ payload }, { call, put }) {
      const response = yield call(getProDetail, payload);
      if (response.success) {
        yield put({
          type: 'getProDetail',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '查询失败：服务端未知错误');
      }
    },
    // 问题人员反馈
    *proFeedBack({ payload, callback }, { call }) {
      const response = yield call(getFeedback, payload);
      if (response.success) {
        if (callback) {
          callback(response);
        }
      } else {
        message.success('反馈失败！');
      }
    },
    // 问题人员反馈并取消标记
    *cancelProbFlag({ payload, callback = () => {} }, { call }) {
      const response = yield call(getFeedback, payload);
      callback(response);
    },
    // 标记问题人员
    *signPro({ payload, callback }, { call }) {
      const response = yield call(signPro, payload);
      if (callback) {
        callback(response);
      }
    },
    // 接单
    *ReceiptList({ payload, callback }, { call }) {
      const response = yield call(reduceemployeeReceipt, payload);
      if (response.success) {
        if (callback) {
          callback(response);
        }
      }
    },
    // 通知签约方
    *noticeSign({ payload, callback }, { call }) {
      const response = yield call(noticeSign, payload);
      if (response.success) {
        if (callback) {
          callback(response);
        }
      }
    },
    // 取消接单
    *cancelReceive({ payload }, { call }) {
      const response = yield call(cancelReceive, payload);
      return response;
    },
    // 产品险种的取消回单 author-xxl
    *cancelReturnReceipt({ payload, callback }, { call }) {
      const response = yield call(reduceCancelReceipt, payload);
      return callback && callback(response);
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 回单
    receiptinfacemessage(
      state,
      {
        payload: { data },
      }
    ) {
      return {
        ...state,
        datar: data,
      };
    },
    // 获取问题人员列表
    getProList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        proListData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 问题人员详情
    getProDetail(state, { payload }) {
      return {
        ...state,
        proBaseList: payload,
        proFbList: payload.probFdbkVos,
      };
    },
    // 发单增员 - 详情
    orderDetails(state, { payload }) {
      let num = 0;
      num = Object.keys(payload).length;
      const temp = payload;
      temp.coltFlag = 1; // 是否需要材料信息
      return {
        ...state,
        detailsData: payload,
        navNum: num,
      };
    },
    // 取消分项
    newpdInfoDtosList(state, { payload }) {
      const temp = state;
      temp.datar.ordCutPdList = payload;
      return {
        ...state,
        datar: state.datar,
      };
    },
    // 分项
    addordCutPdList(state, { payload }) {
      const temp = state;
      temp.datar.ordCutPdList = payload;
      return {
        ...state,
        datar: state.datar,
      };
    },
    // 标记问题人员的问题对象列表
    getProInsuranceList(state, { payload }) {
      return {
        ...state,
        proInsuranceList: payload.data,
      };
    },

    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    controlNavnum(state) {
      const temp = state;
      temp.detailsData.coltFlag = 1;
      return {
        ...state,
        navNum: '1',
        detailsData: state.detailsData, // 是否需要材料信息
      };
    },
  },
};
